package org.esjsoftware.v2m.rim.ui;

import javax.microedition.lcdui.Font;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Characters;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FontFamily;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.PopupScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;

/**
 * 
 * @author Ernest
 *
 */
public class BasePopupScreen extends PopupScreen {

    protected LabelField title;

    public BasePopupScreen() {
            super(new VerticalFieldManager(), Field.FOCUSABLE);
            buildTitle();
            buildSeparatorField();
            buildMainField();
    }

    protected void buildMainField() {
    }

    protected void paint(Graphics graphics) {
        Bitmap backgroundBitmap = Bitmap.getBitmapResource("ChoosePhoneNumber.png");
        graphics.drawBitmap(5, 0, 24, 24, backgroundBitmap, 0, 0);
        super.paint(graphics);
    }
    
    private void buildTitle() {
            HorizontalFieldManager cfm = new HorizontalFieldManager(
                            HorizontalFieldManager.FIELD_HCENTER);
            //cfm.setMargin(0, 0, 5, 0);
            title = new LabelField("Test");
            try {
				title.setFont(FontFamily.forName("BBAlpha Sans").getFont(Font.SIZE_MEDIUM, 26));
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
            cfm.add(title);
            add(cfm);
    }
    
    protected void buildSeparatorField(){
            VerticalFieldManager sep = new VerticalFieldManager(
                            VerticalFieldManager.FIELD_HCENTER);
            sep.setMargin(0, 0, 3, 0);
            SeparatorField sepField = new SeparatorField(){
                protected void layout(int width,int height){    
                    if(getSeparatorWidth() != 0)
                        width = getSeparatorWidth();
                    super.layout(width,height);
                }
            };
            sep.add(sepField);
            add(sep);
    }
    
    protected int getSeparatorWidth(){
        return 0;
    }
    
    public boolean keyChar(char key, int status, int time) {
            boolean retval = false;
            switch (key) {
            case Characters.ESCAPE:
                    close();
                    break;
            default:
                    retval = super.keyChar(key, status, time);
            }
            return retval;
    }

    public void close() {
            UIManager.hideScreen(this);
    }

}
